package cc.linker.steplink.crm.merchanth5.controller.integral;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.pojo.CheckMerchantOrderPo;
import cc.linker.steplink.crm.merchanth5.pojo.integral.CheckIntegralOrderPo;
import cc.linker.steplink.crm.merchanth5.service.ZsGoodsService;
import cc.linker.steplink.crm.merchanth5.service.integral.IntegralH5Service;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分商城
 */
@Slf4j
@RestController
@RequestMapping("/h5/integralMall")
public class IntegralH5Controller {

    @Autowired
    private RequestFilter requestFilter;

    @Autowired
    private IntegralH5Service integralH5Service;

    @Autowired
    private ZsGoodsService zsGoodsService;


    /**
     * 获取积分商品详情
     * @param goodsId
     * @return
     */
    @GetMapping("/goodsInfo")
    public Object getIntegralGoodsInfo(HttpServletRequest request, Long goodsId,
                                       @RequestParam(defaultValue = "") String shareCustomerId,
                                       @RequestParam(defaultValue = "1") Integer shareType){
        WxUser wxUser = requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        zsGoodsService.setGoodsShareLog(goodsId,shareCustomerId,shareType,wxUser.getCustomerId());
        return integralH5Service.getIntegralGoodsInfo(goodsId,orgId,wxUser.getCustomerId());
    }

    /**
     * 确认订单
     *
     * @param : goodsId
     * @param : skuId
     * @param : buyCount
     */
    @PostMapping("/checkorder")
    public Map<String,Object> checkOrder(@RequestBody Map<String, Object> param, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        param.put("orgId", orgId);
        Map<String,Object> data = new HashMap<>();
        List<CheckIntegralOrderPo> checkMerchantOrderPos = integralH5Service.checkOrder(param);
        int totalNum = 0;
        for (CheckIntegralOrderPo da : checkMerchantOrderPos) {
            totalNum = totalNum + da.getGoodsNum();
        }
        data.put("totalNum",totalNum);
        data.put("orderList",checkMerchantOrderPos);
        return data;
    }

    /**
     * 确认知识付费订单
     *
     * @param : goodsId
     */
    @PostMapping("/knowledge/checkorder")
    public Map<String, Object> checkKnowledgeOrder(@RequestBody Map<String, Object> param, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        param.put("orgId", orgId);
        return integralH5Service.checkKnowledgeOrder(param);
    }

    /**
     * 创建订单
     *
     * @param : addressId       地址id
     * @param : goodsId         商品id(分小b商品和大b商品)
     * @param : goodsNum        商品数量
     * @param : skuId           skuId
     * @param : remark          备注
     */
    @PostMapping("/createorder")
    public Object createOrder(@RequestBody Map<String, Object> param, HttpServletRequest request) throws GlobalException {
        LOGGER.info("下单参数===[{}]", param);
        // 创建订单，生成返润体系，然后调中金得到支付签名，返回给前端
        WxUser wxUser = requestFilter.getWxUser(request);
        param.put("customerId", wxUser.getCustomerId());
        param.put("orgId", wxUser.getOrgId());
        param.put("openid", wxUser.getPayOpenId());
        param.put("payMode", wxUser.getPayMode());
        //WxUser wxUser =  requestFilter.getWxUser(request);
        return integralH5Service.createOrder(param);
    }


    /**
     * 商品限购判断
     *
     * @param goodsId 商品id，多个用逗号拼接
     */
    @GetMapping("/quota")
    public Object quota(Long goodsId,Long skuId, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();
        return integralH5Service.quota(goodsId,skuId,customerId,orgId);
    }

    /**
     * 确认收货
     *
     * @param userId
     * @param orderId
     */
    @PostMapping("/checkReceiveOrder")
    public void checkReceiveOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        params.put("orgId", wxUser.getOrgId());
        params.put("payMode", wxUser.getPayMode());
        integralH5Service.checkReceiveOrder(params);
    }

    @PostMapping("/editorderaddress")
    public void editOrderAddress(HttpServletRequest request,@RequestBody Map<String, Object> param){
        if(StringUtils.isEmpty(param.get("orderSn"))||StringUtils.isEmpty(param.get("addressId"))){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        integralH5Service.editOrderAddress(param);
    }
}
